package controler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import castleContent.User;
import castleContent.Users;
import dataBase.DataBase;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String username;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		username = request.getParameter("RegUserName");
		String age = request.getParameter("RegAge");
		String password = request.getParameter("RegPass");
	    String race = request.getParameter("SelectRace");
	    String city = request.getParameter("SelectCity");

			if(isValidRegistering(username, password)){
				Users.users.put(username, new User(username, age, password, race, city));
				response.sendRedirect("Login.html");	
				DataBase.initializeDataBase();				
			}
			else{
				response.sendRedirect("RegisterServlet");
			}

	}

	private boolean isValidRegistering(String username, String password) {		
		return username != null && !username.equals("") && password != null && !password.equals("");
	}

	public static String getUsername() {
		return username;
	}

}
